/**
 * Copyright (C) 2018 - 2020 https://github.com/LeeYan9
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bifrost.web.controller;

import com.bifrost.annotation.Authorization;
import com.bifrost.annotation.CurrentStore;
import com.bifrost.annotation.CurrentUser;
import com.bifrost.common.util.HttpRequestUtils;
import com.bifrost.entity.Customer;
import com.bifrost.entity.Plan;
import com.bifrost.model.PlanCommitModel;
import com.bifrost.model.ResultModel;
import com.bifrost.model.vo.PageVO;
import com.bifrost.service.CustomerService;
import com.bifrost.service.PlanService;
import com.google.common.collect.Lists;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Lee
 * Create Class on 2018/8/14
 * @com.bifrost.web.controller
 * @wechat-mall wechat-Version --申领活动
 */
@RequestMapping("/api/plan")
@RestController
@Api("/前台申领活动相关接口")
public class PlanController {

    @Autowired
    PlanService planService;
    @Autowired
    CustomerService customerService;

    @GetMapping("/page")
    @ApiOperation(value = "/分页获取所有申领活动",notes = "获取所有申领活动" )
    @ApiImplicitParams({@ApiImplicitParam(name = "page" , value = "当前页" , dataType = "integer" , paramType = "query" , required = true , defaultValue = "1"),
            @ApiImplicitParam(name = "size" , value = "页数" , dataType = "integer" , paramType = "query" , defaultValue = "8")})
    public ResultModel page(@CurrentStore Long storeId, @RequestParam(value = "page",defaultValue = "1")Integer page , @RequestParam(value = "size",defaultValue = "8") Integer size ,@RequestParam(value = "planType",defaultValue = "TRYOUT")String planType , Date start , Date end , String storeIds , String status){
        Page<Plan> plans = planService.findPage(storeId, storeIds, page, size, planType, start, end==null?new Date():end,status);
        ArrayList<Map> maps = Lists.newArrayList();
        plans.forEach(plan -> {
            HashMap<String, Object> map = new HashMap<>(5);
            map.put("planId",plan.getPlanId());
            map.put("title",plan.getTitle());
            map.put("status",plan.getStatus());
            map.put("url", HttpRequestUtils.getUrl(plan.getUrl()));
            map.put("type",plan.getPlanType());
            map.put("description",plan.getDescription());
            map.put("startTime",plan.getActiveStartTime());
            map.put("endTime",plan.getActiveEndTime());
            maps.add(map);
        });
        return ResultModel.ok().put("page",new PageVO(page,size,(int)plans.getTotalElements(),maps));
    }

    @GetMapping("/main/detail")
    @ApiOperation(value = "/获取申领活动下的申领商品详情",notes = "获取申领详情" )
    @Authorization
    public ResultModel itemDetail(@ApiParam(name = "planId", value = "申领活动Id", required = true) @RequestParam("planId") Long planId , @CurrentUser Long customerId){
        return ResultModel.ok().put("planDetail",planService.planDetail(planId,customerService.findCustomer(customerId)));
    }

    @GetMapping("/winners")
    @ApiOperation(value = "/获奖名单",notes = "获奖名单" )
    @Authorization
    public ResultModel itemDetail( @RequestParam("planItemId") Long planItemId){
        return ResultModel.ok().put("data",planService.winners(planItemId));
    }

    @PostMapping("/apply")
    @ApiOperation(value = "/申请名额",notes = "申请名额" )
    @Authorization
    public ResultModel apply(@CurrentUser Long userId , @RequestBody @Valid PlanCommitModel commitModel){
        planService.commitApply(commitModel,customerService.findCustomer(userId));
        return ResultModel.ok("申请成功");
    }

    @PostMapping("/planCustomer/detail")
    @ApiOperation(value = "/用户申领列表",notes = "用户申领列表" )
    @Authorization
    public ResultModel apply(@CurrentUser Long userId , String status){
        return ResultModel.ok().put("data",planService.customerDetail(customerService.findCustomer(userId) , status));
    }

    @PostMapping("/planCustomer/feedback")
    @ApiOperation(value = "/用户申领",notes = "用户申领" )
    @Authorization
    public ResultModel apply(String feedbackData , Long planCustomerId){
        planService.saveFeedback(feedbackData,planCustomerId);
        return ResultModel.ok("反馈结果已提交");
    }

}
